package controllor

import (
	"fitness/internal/service"
	"fitness/pkg/middleware"
	"fitness/pkg/tools"
	"fitness/pkg/utils/R"
	"github.com/gin-gonic/gin"
	"net/http"
)

type AuthController struct {
	LoginService *service.LoginService
}

func NewAuthController(loginService *service.LoginService) *AuthController {
	return &AuthController{
		LoginService: loginService,
	}
}

func (authApi *AuthController) InitApiRouter(engine *gin.Engine) {
	auth := engine.Group("/auth")
	{
		//验证码
		auth.GET("/captcha", GetCaptchaV1)
		//后台用户登录
		auth.POST("/login", authApi.LoginService.CheckPassword)
		//退出登陆
		auth.DELETE("/logout", middleware.Jwt, authApi.LoginService.Logout)
	}
}

// 生成base64的验证码
func GetCaptchaV1(c *gin.Context) {
	id, base64, _, err := tools.CreateImageCaptcha()
	if err != nil {
		c.JSON(http.StatusInternalServerError, R.ReturnSuccessMsg("验证码生成错误"))
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "一切ok",
		"data": gin.H{
			"captchaBase64":  base64,
			"captchaEnabled": true,
			"captchaKey":     id,
		},
	})
}
